package com.antdesignbackend.backend.activiti.processlist.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.activiti.processlist.entity.ProcessList;
import com.antdesignbackend.backend.activiti.processlist.entity.entityVo.ProcessListVo;
import com.antdesignbackend.backend.activiti.processlist.service.ProcessListService;

/**
 * 配置流程
 *
 * @author 不二
 * @email processlist
 * @date 2020-08-11 16:53:34
 */

@Controller
@RequestMapping("/processList")
@Slf4j
public class ProcessListController extends BaseController{

	private static final String FUN_NAME = "配置流程";//功能名称

	private Integer processId;

	@Autowired
	private ProcessListService processListService;

	@GetMapping("/index")
	@AuthToken
	public String index(Integer processId) {
		this.processId = processId;
		return "activiti/processList/index";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody ProcessListVo processListVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		List<ProcessListVo> list = new ArrayList<>();
		QueryWrapper<ProcessListVo> wrapper = new QueryWrapper<>();
		wrapper.eq("processId",this.processId);
		wrapper.orderByAsc("Sort");
		int total = processListService.count(wrapper);
		if (total > 0){
			list = processListService.search(wrapper,new Page<ProcessListVo>(processListVo.getPageIndex(),processListVo.getPageSize()));
		}
		result.setData(list);
		result.setTotal(total);
		return result;
	}

	@GetMapping("/getById/{id}")
	@ResponseBody
	public	ProcessList getById(@PathVariable(name = "id") String id) {
		ProcessList obj = processListService.getById(id);
		return obj;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody ProcessList processList){
		ReturnVO result = new ReturnVO();
		processList.setCreateBy(getLoginUserName());
		processList.setCreateTime(new Date());
		processList.setProcessId(this.processId);
		if(processListService.save(processList)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody ProcessList processList){
		ReturnVO result = new ReturnVO();
		processList.setUpdateBy(getLoginUserName());
		processList.setUpdateTime(new Date());
		if(processListService.updateById(processList)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (processListService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}

}
